<?php


namespace Frieza\Modules\Photo\Controllers;

use App\Http\Controllers\Controller;
use Doraemon\cache\Photos as cPhotos;
use Doraemon\cache\Queue;
use Doraemon\model\AdmNoteOutlineLog;
use Doraemon\model\Photos;
use Doraemon\model\TopicActivity;
use Doraemon\service\AdmNote;
use Doraemon\service\es\EsPhoto;
use Doraemon\service\RecommendQuestion313;
use Doraemon\service\User;
use Doraemon\tools\Link;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Doraemon\tools\Hhzurl;

class OperateController extends Controller
{
    const RECOMMEND_ANSWER_IN_ALL = '/home/adm_rsync_dir/recommend_location_topic_in_all.dic';
    public function operate(Request $request)
    {
        $code = 1;
        $msg = '操作成功';
        try {
            $params = $request->all();
            $noteId = $params['note_id'];
            $type = $params['type'];
            $extraMsg = $params['extra_msg'];

            if (!$noteId) {
                throw new \Exception('参数错误');
            }
            if (!in_array($type,
                ['invisible', 'bottom', 'unbottom', 'fold', 'unfold', 'remove','sethot','cancelhot'])) {
                throw new \Exception('操作错误');
            }
            call_user_func([$this, $type], $noteId, $extraMsg);
        } catch (\Exception $e) {
            $code = 0;
            $msg = $e->getMessage();
        }
        return compact('code', 'msg');
    }

    private function sethot($noteId)
    {
        $photoInfo = Photos::getPhoto($noteId);
        $uid = $photoInfo['uid'];
        $userInfo = User::getBasicUserInfoByCache($uid);
        $nickName =$userInfo['nick'];

        $data['is_best'] = Photos::IS_BEST_YES;
        $data['edittime'] = time();
        Photos::updatePhotoById($noteId,$data);
        //添加到队列，更新搜索图片
        Queue::insertQueue('Photo/afterAudit', [
            'photo_id' => $noteId,
        ]);
        \Doraemon\cache\Photos::delete($noteId);

        //下行
        Queue::insertQueue('queue/Sysmsg/Send', [
            'msgCode' => 770,
            'to_uid' => $uid,
            'msgParams' => [
                'nickName' => $nickName,
                'link' => \Doraemon\tools\HhzNiceLivingUrl::getNoteDetailLink($noteId)
            ]
        ], Queue::QUEUE_DORAEMON);
    }

    private function cancelhot($noteId)
    {
        $data['is_best'] = Photos::IS_BEST_NO;
        $data['edittime'] = time();
        Photos::updatePhotoById($noteId,$data);
        Queue::insertQueue('Photo/afterAudit', [
            'photo_id' => $noteId,
        ]);
        \Doraemon\cache\Photos::delete($noteId);
    }

    private function invisible($noteId)
    {
        $data['status'] = Photos::STATUS_PRIVATE;
        $data['show_status'] = Photos::SHOW_STATUS_INVISIBLE;
        $data['admin_score'] = 4;
        $data['edittime'] = time();
        $rs = Photos::updatePhotoById($noteId, $data);
        if ($rs) {
            // 添加操作日志
            AdmNote::auditLog($noteId, Photos::QUALITY_LOW, Admin::user()->name, AdmNoteOutlineLog::AUDIT_CHANNEL_LOW_NOTE);
            // 删除图片信息缓存
            cPhotos::delete($noteId);
            Queue::insertQueue('Photo/afterAudit', [
                'photo_id' => $noteId,
            ]);
        }
    }

    private function bottom($noteId)
    {
        if(file_exists(self::RECOMMEND_ANSWER_IN_ALL)) {
            $answers = file_get_contents(self::RECOMMEND_ANSWER_IN_ALL);
            if(strpos($answers,$noteId)){
                throw new \Exception('该内容已存在固定位回答中，不可置底');
            }
        }
        $photoInfo = Photos::getPhoto($noteId);
        if ($photoInfo && isset($photoInfo['topic_activity_id']) &&
            $photoInfo['topic_activity_id'] && $photoInfo['is_best'] == Photos::IS_BEST_YES) {
            RecommendQuestion313::delHotBestAnswer($photoInfo['topic_activity_id'], $noteId);
        }

        Photos::updatePhotoById($noteId,
            ['is_bottom' => Photos::IS_BOTTOM_YES, 'is_best' => Photos::IS_BEST_NO, 'edittime' => time()]);

        // 添加到队列，更新搜索图片
        Queue::insertQueue("Photo/afterAudit", ['photo_id' => $noteId]);

        \Doraemon\cache\Photos::delete($noteId);
    }

    private function unbottom($noteId)
    {
        Photos::updatePhotoById($noteId,
            ['is_bottom' => Photos::IS_BOTTOM_NO, 'is_best' => Photos::IS_BEST_NO, 'edittime' => time()]);

        \Doraemon\cache\Photos::delete($noteId);
        // 添加到队列，更新搜索图片
        Queue::insertQueue("Photo/afterAudit", ['photo_id' => $noteId]);
    }

    //折叠回答
    //Admin/Photos/foldAnswer
    private function fold($noteId, $extraMsg)
    {
        Photos::updatePhotoById($noteId, ['is_fold' => Photos::IS_FOLD_YES, 'edittime' => time()]);// 删除图片信息缓存
        cPhotos::delete($noteId);
        Queue::insertQueue("Photo/afterAudit", ['photo_id' => $noteId]);

        $photoInfo = Photos::getPhoto($noteId);
//        $topicInfo = TopicActivity::getById($photoInfo['topic_activity_id']);
//        $userInfo = User::getUserInfo($photoInfo['uid']);
        //发下行
        if (isset($extraMsg) && $extraMsg) {
            if ($extraMsg == 'not_related') {
                $msg = "住友你好，你发布的内容与关联的话题主题不符，根据《好好住社区规范》该内容将不在话题内展示哦～如有异议请发邮件至hello@haohaozhu.com 与我们联系，感谢对好好住的支持和理解。！";
            } else {
                $msg = "住友你好，你发布的内容涉及违规，根据《好好住社区规范》该内容将不在话题内展示哦～如有异议请发邮件至hello@haohaozhu.com 与我们联系，感谢对好好住的支持和理解。";
            }
            $jump = \Doraemon\tools\HhzNiceLivingUrl::getNoteDetailLink($noteId);
            if (empty($photoInfo['uid'])) {
                return;
            }
            Queue::insertQueue('queue/Sysmsg/Send', [
                'msgCode' => 0,
                'to_uid' => $photoInfo['uid'],
                'msgParams' => [
                    'link' => $jump,
                    'detail' => $msg,
                    'html_detail' => $msg,
                ],
            ], Queue::QUEUE_DORAEMON);

        }
    }

    //取消折叠回答
    private function unfold($noteId)
    {
        Photos::updatePhotoById($noteId, ['is_fold' => Photos::IS_FOLD_NO, 'edittime' => time()]);// 删除图片信息缓存
        cPhotos::delete($noteId);
        Queue::insertQueue("Photo/afterAudit", ['photo_id' => $noteId]);
    }

    //Admin/Photos/removeOfAnswer
    private function remove($noteId, $msg)
    {
        $photoInfo = Photos::getPhoto($noteId);

        $data['topic_activity_id'] = 0;
        $data['edittime'] = time();
        $rs = Photos::updatePhotoById($noteId, $data);
        if ($rs) {
            //回答数减一
            \Doraemon\cache\TopicCounter::decrease($photoInfo['topic_activity_id'],
                \Doraemon\cache\TopicCounter::ANSWER_COUNT);

            cPhotos::delete($noteId);
            Queue::insertQueue("Photo/afterAudit", ['photo_id' => $noteId]);

            //发下行
            if (isset($msg) && $msg) {
                $jump = Link::format(Link::LINK_PHOTO, $noteId);
                Queue::insertQueue('queue/Sysmsg/Send', [
                    'msgCode' => 0,
                    'to_uid' => $photoInfo['uid'],
                    'msgParams' => [
                        'link' => $jump,
                        'detail' => $msg,
                        'html_detail' => $msg,
                    ],
                ], Queue::QUEUE_DORAEMON);
            }
        }
    }
}
